# 糗事百科爬取所有图片
import os
import re

import requests

if __name__ == '__main__':
    # 新建一个文件夹保存文件
    if not os.path.exists('./qiutuLibs'):
        os.mkdir('./qiutulibs')
    # 请求头
    headers = {
        'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"
    }
    # 请求url
    url = 'https://www.qiushibaike.com/imgrank/page/%d/'
    for page_num in range(1,14):
        #拼接url方法
        new_url=format(url%page_num)
        page_text = requests.get(url=url, headers=headers).text
        # 正则匹配
        #TODO ？
        ex = '<div class="thumb">.*?<img src="(.*?)" alt.*?</div>'
        img_src_list = re.findall(ex, page_text, re.S)
        print(img_src_list)
        for src in img_src_list:
            # 拼接完成图片url
            src = 'http:' + src
            # 请求图片二进制数据
            img_date = requests.get(url=url).content
            # 拼接生产图片名称
            img_name = src.split('/')[-1]
            # 保存路径
            img_path = './qiutuLibs/' + img_name
            # 保存
            with open(img_path, 'wb') as fp:
                fp.write(img_date)
            print(img_name, '下载成功')
